package cn.kgc.shiro02.service;

import cn.kgc.shiro02.entity.Role;
import cn.kgc.shiro02.entity.User;
import cn.kgc.shiro02.mapper.UserMapper;
import cn.kgc.shiro02.utils.SaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/6/1
 */
@Service
public class UserServiceImpl  implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public boolean saveUser(User user) {
        //获取用户对象中的密码 进行加密处理
        String password = user.getPassword();

        //获取随机盐
        String salt = SaltUtil.getSalt(5);
        user.setSalt(salt);
        //将加密后的密码设置到user对象
        Md5Hash md5Hash = new Md5Hash(password, salt, 1024);
        String s = md5Hash.toHex();
        user.setPassword(s);
        //密码已加密  随机盐已设置
        System.out.println("user = " + user);
        int i = userMapper.saveUser(user);
        return i>0;
    }

    @Override
    public User selectByUserName(String username) {
        User user = userMapper.selectByUserName(username);
        return user;
    }

    @Override
    public List<Role> selectRolesByUserName(String username) {
        List<Role> roles = userMapper.selectRolesByUserName(username);
        return roles;
    }
}
